package com.xuanwei.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.xuanwei.core.entity.vo.ActivityCouponVo;
import com.xuanwei.core.entity.vo.CouponProductVo;
import com.xuanwei.core.entity.vo.CouponVo;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwCartService;
import com.xuanwei.core.service.IXwCouponService;
import com.xuanwei.core.service.IXwUserCouponService;
import com.xuanwei.dao.entity.XwCart;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/18 14:03
 **/
@RestController
@RequestMapping("wx/coupon")
@Validated
public class CouponController extends BasicController {

    private IXwCouponService couponService;

    private IXwUserCouponService userCouponService;

    private IXwCartService cartService;

    public CouponController(IXwCouponService couponService,
                            IXwUserCouponService userCouponService,
                            IXwCartService cartService) {
        this.couponService = couponService;
        this.userCouponService = userCouponService;
        this.cartService = cartService;
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO list() {
        List<ActivityCouponVo> exceptCoupon = couponService.findExceptCoupon(getUserId());
        List<CouponProductVo> productCoupon = couponService.findProductCoupon(getUserId());
        Map<String, Object> result = new HashMap<>(2);
        result.put("coupons", exceptCoupon);
        result.put("productCoupon", productCoupon);
        return ResultVO.successResult(result);
    }

    @RequestMapping(value = "obtain", method = RequestMethod.GET)
    public ResultVO obtain(@NotNull(message = "id不能为空")
                           @Min(value = 1, message = "id格式错误")
                                   Integer id) {
        couponService.obtainCoupon(getUserId(), id);
        return ResultVO.successResult("领取成功");
    }

    @RequestMapping(value = "choiceCoupon", method = RequestMethod.GET)
    public ResultVO choiceCoupon(String cartId, Integer skuId, Integer count) {
        if (StringUtils.isNotBlank(cartId)) {
            List<Integer> collect = Arrays.stream(cartId.split("-"))
                    .map(Integer::parseInt).collect(Collectors.toList());
            List<XwCart> carts = cartService.list(new QueryWrapper<XwCart>().in("id", collect));
            Map<String, Object> couponVos = userCouponService.choiceList(getUserId(), carts);
            return ResultVO.successResult(couponVos);
        }
        if (skuId != null && count != null) {
            XwCart cart = cartService.generateCart(skuId, count);
            ArrayList<XwCart> carts = Lists.newArrayListWithCapacity(1);
            carts.add(cart);
            Map<String, Object> couponVos = userCouponService.choiceList(getUserId(), carts);
            return ResultVO.successResult(couponVos);
        }

        return ResultVO.failResult("参数错误");
    }

    @RequestMapping(value = "userCoupon", method = RequestMethod.GET)
    public ResultVO userCoupon(Integer type) {
        List<CouponVo> couponVos = userCouponService.userCouponList(getUserId(), type, getPage());
        return ResultVO.successResult(couponVos);
    }

}
